<?php


namespace app\admin\controller;
use app\common\model\GoodsCate as GC;
use app\common\model\Attr as AT;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
class GoodsCate extends Base
{
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');

        $list = GC::where($where)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('image', "%$keyword%");
                }
            })
            ->field('id as gcate_id,goodscatename,keywords,description,parentid,Icon,sort,url,image,is_menu,is_next,is_blank,status,create_time,update_time,son,sonsw,template_list,template_show,attr_id')
            ->select();
        //dump($list);die;
        $cate = goodscate_tree($list);
        //dump($cate);die;

        $view = [
            'keyword' => $keyword,
            'list' => $cate,
            'pid' => $pid,
        ];
        View::assign($view);
        return View::fetch();
    }

    public function add(){
        //获取栏目列表
        $goodscate = GC::field('id as gcate_id,goodscatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        //dump($goodscate);die;
        $category = goodscate_tree($goodscate);
        //获取默认上级
        $id = Request::param('id',0);

        $pid = Request::param('pid',0);
        //包含属性
        $attr_info = AT::where('group_status=1')->select();
        //获取所有模版
        $template = getTemplate();
        $view = [
            'category' => $category,
            'id' => $id,
            'pid' => $pid,
            'template' => $template,
            'attr_info'=>$attr_info,
            'type'=>'添加商品分类',
            'info' => null
        ];
        View::assign($view);
        return View::fetch();
    }

    public function addPost(){
        if(Request::isPost()){
            $data = Request::post();
            //p($data);die;
            $data['create_time'] = time();
            $data['attr_id'] = implode(',',$data['attr_id']);
            if (GC::create($data)) {
                $this->success('添加成功', 'index');
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function edit(){
        //获取栏目列表
        $goodscate = GC::field('id as gcate_id,goodscatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $category = goodscate_tree($goodscate);
        //获取默认上级
        $id = Request::param('id',0);
        //dump($id);die;
        $info = GC::where('id',$id)->find();
        //dump($info);die;
        $pid = Request::param('pid',0);
        //包含属性
        $attr_info = AT::where('group_status=1')->select();
        //dump($attr_info);die;
        //获取所有模版
        $template = getTemplate();
        $view = [
            'category' => $category,
            'id' => $id,
            'pid' => $pid,
            'template' => $template,
            'attr_info'=>$attr_info,
            'type'=>'添加商品分类',
            'info' => $info,
        ];
        View::assign($view);
        return View::fetch('add');
    }

    public function editPost(){
        if(Request::isPost()){
            $data = Request::post();
            $data['attr_id'] = implode(',',$data['attr_id']);
            if ($data['parentid'] == $data['id']){
                $this->error('栏目同级不能成为上下级关系');
            }
            $data['update_time'] = time();
            $where['id'] = $data['id'];
            if (GC::update($data,$where)) {
                $this->success('修改成功', 'index');
            } else {
                $this->error('修改失败');
            }
        }
    }

    public function cate_del(){
        $get = Request::param('cate_id');
        //dump($get);die;
        $cateId = Db::name('goods_cate')->field(array('id','parentid'))->select();
        //dump($cateId);die;
        $delId = get_all_child($cateId,$get);
        //dump($delId);die;
        $delId[] = $get;
        $where = array(
            'id' => $delId,
        );
        foreach ($where as $k => $v){
            $categorypic = GC::where(array('id'=>$v))->find();
        }

        if(GC::where($where)->delete()){
            return success('删除成功','index');
        }else{
            return error('删除失败！');
        }

    }

    public function del(){
        $id = Request::param('id','0');
        $ids = explode(',',$id);
        $cateId = GC::field(array('id','parentid'))->select();
        //dump($cateId);die;
        foreach ($ids as $k =>$v){
            $delId = get_all_child($cateId,$v);
            $delId[] = $v;
            $where = array(
                'id' => $delId,
            );
            //dump($delId);die;
            $resuld = GC::where($where)->delete();
        }
        if($resuld){
            return success('批量删除成功','index');
        }else{
            return error('批量删除失败！');
        }
    }


    public function ajaxCate(){
        $key = Request::param('name');
        $value = Request::param('param');
        $id = input('id', 0, 'intval');
        $cateadmin = new GC();
        $res = $cateadmin->ajaxCateadmin($value, $id);

        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '商品分类名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }
}